//
//  NSData+divisionTime.m
//  kangzhipifuyisheng
//
//  Created by 唐开江 on 15/7/21.
//  Copyright (c) 2015年 KangZhi. All rights reserved.
//

#import "NSDate+divisionTime.h"

@implementation NSDate (divisionTime)

- (int)getDay
{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    int unit = NSCalendarUnitDay | NSCalendarUnitMonth |  NSCalendarUnitYear;
    
    // 2.获得self的年月日
    NSDateComponents *selfCmps = [calendar components:unit fromDate:self];
    
    return selfCmps.day;
    
}
- (int)getMonth
{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    int unit = NSCalendarUnitDay | NSCalendarUnitMonth |  NSCalendarUnitYear;
    
    // 2.获得self的年月日
    NSDateComponents *selfCmps = [calendar components:unit fromDate:self];
    
    return selfCmps.month;

}
- (int)getYear
{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    int unit = NSCalendarUnitDay | NSCalendarUnitMonth |  NSCalendarUnitYear;
    
    // 2.获得self的年月日
    NSDateComponents *selfCmps = [calendar components:unit fromDate:self];
    
    return selfCmps.year;
}


+ (NSDate *)dateFromString:(NSString *)dateString  withFormat:(NSString *)formatStr
{

    NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat: formatStr];
    NSDate *firstDay= [dateFormatter dateFromString:dateString];
    return firstDay;
}

//获取某年某月的总天数
+ (int)daysInMonth:(int)month year:(int)year
{

//    NSCalendar
    NSDate *today = [NSDate date]; //Get a date object for today's date
    NSCalendar *c = [NSCalendar currentCalendar];
    NSRange days = [c rangeOfUnit:NSCalendarUnitDay
                           inUnit:NSCalendarUnitMonth
                          forDate:today];
    return days.length;

//    return 0;
}




@end
